// miniprogram/pages/test/admin/admin.js
const answer_db = wx.cloud.database().collection("testAnswer")

Page({

  data: {
    answerList: []
  },
  onLoad: function (options) {
    var testItem = JSON.parse(decodeURIComponent(options.testItem))
    // console.log(testItem)
    wx.setNavigationBarTitle({
      title: testItem.TestName + '考试情况'
    })
  },
  onShow: function () {
    this.getUserAnswer()
  },
  onShareAppMessage: function () {

  },
  //中心点击监听
  onClassClick(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var answerData = this.data.answerList[index]
    var answerId = answerData._id
    console.log(answerData)
    console.log(answerId)
    if (answerData.testStatus == '已判分') {
      wx.navigateTo({
        url: '../answer/answer?publishId=' + answerId,
      })
    } else {
      wx.navigateTo({
        url: '../answer/answer?flag=editScore&publishId=' + answerId,
      })
    }
  },
  //获取用户分数
  getUserAnswer(e) {
    wx.cloud.callFunction({
      name: "database",
      data: {
        action: 'getUserAnswers'
      }
    }).then(res => {
      console.log(res)
      var answerList = res.result.allAnswers
      this.setData({
        answerList: answerList
      })
    })
  },
  //删除按钮点击监听
  onDeleteClick(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var answerData = this.data.answerList[index]
    var answerId = answerData._id
    wx.showModal({
      title: '提示',
      content: '用户试卷会被删除，确定删除吗?',
      showCancel: true,
      success: res => {
        if (res.confirm) {
          console.log('用户点击确定')
          this.deleteAnswerById(answerId)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除试卷
  deleteAnswerById(answerId) {
    wx.cloud.callFunction({
      name: "database",
      data: {
        action: 'deleteAnswerById',
        answerId: answerId
      }
    }).then(res => {
      // console.log(res)
      var answerList = res.result.allAnswers
      this.setData({
        answerList: answerList
      })
      //删除结果
      var deleteResult = res.result.deleteResult
      console.log("deleteResult", deleteResult)
      if (deleteResult.stats.removed == 1) {
        wx.showToast({
          title: '删除成功',
          icon: "none"
        })
      }
    })
  }
})